<div>
<script type="text/javascript" charset="utf-8" src="/admin/ueditor.config.js?path=text"></script>
<script type="text/javascript" charset="utf-8" src="/ueditor/ueditor.all.min.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="/ueditor/lang/zh-cn/zh-cn.js"></script>
<style type="text/css">
.edui-default {/*z-index: 9005!important;*/}
.modal{z-index: 999;}
.modal-backdrop{z-index: 998;}
.nmenu{z-index: 997;}
.navbar-fixed-top{z-index: 997;}
</style>
    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">基本信息</a></li>
        <li role="presentation"><a href="#introduce" aria-controls="introduce" role="tab" data-toggle="tab">商品介绍</a></li>
        <li role="presentation"><a href="#imgurl" aria-controls="imgurl" role="tab" data-toggle="tab">商品图片</a></li>
        <li role="presentation"><a href="#attribute" aria-controls="attribute" role="tab" data-toggle="tab">商品属性</a></li>
        <li role="presentation"><a href="#spec" aria-controls="spec" role="tab" data-toggle="tab">商品规格</a></li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content" style="width: 90%;">
        <div role="tabpanel" class="tab-pane active" id="home">
            <div class="form-group" style="text-align: center;" id="catimg">
                        
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">商品分类：</label>
              <div class="col-sm-9">
                 <select class="form-control" name="upperid">
                    {% for i, j in class sorted %}
                        <option id="{{ j.id }}" value="{{ j.id }}">{{ mstr(j.level, "&nbsp;&nbsp;&nbsp;&nbsp;")|safe }}{{ j.name|safe }}</option>
                    {% endfor %}
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">商品编号：</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" name="sn" placeholder="如果不填，将自动生成" required>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">商品名称：</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" name="goodsname" required>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">副标题：</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" name="goodstitle" required>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">销售价：</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" name="price" onkeyup="checkNum(this)" required>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">市场价：</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" name="vprice" onkeyup="checkNum(this)" required>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">成本价：</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" name="oprice" onkeyup="checkNum(this)" required>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">库存：</label>
              <div class="col-sm-9">
                 <input type="number" class="form-control" name="kc" required>
              </div>
            </div>
            <!-- <div class="form-group">
              <label for="imgurl" class="col-sm-3 control-label">展示图片：</label>
              <div class="col-sm-9">
                 <input type="file" class="form-control" id="uimgurl" name="imgurl" runat="server" onchange="previewImage(this, 'catimg', 100, 100)" required>
              </div>
            </div> -->
            <div class="form-group">
              <label class="col-sm-3 control-label">备注：</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" id="ubar" name="remark" >
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-3 control-label">设置：</label>
              <div class="col-sm-9">
                <label style="margin-right: 10px;">
                    <input type="checkbox" name="isground" value="true" checked="checked" />是否上架
                    <input type="hidden" name="_isground" value="false" />
                </label>
                <label style="margin-right: 10px;">
                    <input type="checkbox" name="islines" value="true" checked="checked" />是否上线
                    <input type="hidden" name="_islines" value="false" />
                </label>
              </div>
            </div>
        </div>
        <div role="tabpanel" class="tab-pane" id="introduce">
            <div class="form-group">
                <label class="col-sm-3 control-label">商品介绍：</label>
                    <div class="col-sm-9">
                    <div>
                        <script id="editor" type="text/plain" style="width:100%;height:450px;"></script>
                    </div>
                </div>
                <script type="text/javascript">
                    var ue = UE.getEditor('editor');
                </script>
            </div>
        </div>
        <div role="tabpanel" class="tab-pane" id="imgurl">
            <div class="form-group">
                <label class="col-sm-3 control-label">商品图片：</label>
                <div class="col-sm-9" id="bpimg">
                </div>
                <script type="text/javascript">
                    //UploadImg.getData('bpimg')
                    UploadImg.create('bpimg', '/admin/', function(data){
                        // alert()
                    }, "goodimg");
                </script>
            </div>
        </div>
        <div role="tabpanel" class="tab-pane" id="attribute">
            <div style="margin-top: 20px;" class="battribute">
                <div class="form-group">
                  <label class="col-sm-3 control-label">商品属性：</label>
                  <div class="col-sm-4">
                     <input type="text" class="form-control" name="attributename" placeholder="属性名" >
                  </div>
                  <div class="col-sm-4">
                     <input type="text" class="form-control" name="attribute" placeholder="属性值" >
                  </div>
                </div>
            </div>
            <div style="text-align: right;">
                <button type="button" class="btn btn-primary addattribute">添加属性</button>
            </div>
            <script type="text/javascript">
                $('.addattribute').click(function(){
                    $('.battribute').append('<div class="form-group"><label class="col-sm-3 control-label">商品属性：</label><div class="col-sm-4"><input type="text" class="form-control" name="attributename" placeholder="属性名" ></div><div class="col-sm-4"><input type="text" class="form-control" name="attribute" placeholder="属性值" ></div></div>')
                });
            </script>
        </div>
        <div role="tabpanel" class="tab-pane" id="spec">
            <div style="margin-top: 20px;">
                <div class="form-group">
                  <label class="col-sm-3 control-label">商品规格：</label>
                  <div class="col-sm-9">
                    <select class="form-control" onchange="spechange(this)">
                        <option></option>
                        {% for i, j in class sorted %}
                            <option id="{{ j.id }}" value="{{ j.id }}">{{ mstr(j.level, "&nbsp;&nbsp;&nbsp;&nbsp;")|safe }}{{ j.name|safe }}</option>
                        {% endfor %}
                    </select>
                    <div style="padding: 10px;" class="col-sm-12" id="aformat"></div>
                    <div>&nbsp;<hr></div>
                    <div>
                        <table class="table table-hover">
                            <thead id="saddf"></thead>
                            <tbody id="saddb"></tbody>
                        </table>
                    </div>
                  </div>
                </div>
                <style type="text/css">
                .gt {
                    width: 70px;
                    height: 30px;
                    line-height: 30px;
                    margin-left: 5px;
                    margin-right: 5px;
                    padding: 5px;
                    color: #666666;
                    -webkit-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.075);
                    -moz-box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.075);
                    box-shadow: inset 0px 1px 1px rgba(0, 0, 0, 0.075);
                    -webkit-transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s;
                    -moz-transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s;
                    transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s;
                    -webkit-border-radius: 3px;
                    -moz-border-radius: 3px;
                    border-radius: 3px;
                    border: 1px solid;
                    border-color: #999999 #e1e1e1 #e1e1e1 #999999;
                }
                th{text-align: center;line-height: 47px;}
                td{text-align: center;line-height: 47px;}
                </style>
                <script type="text/javascript">
                    igft = {};
                    spechange = function(t){
                        igft = {};
                        palert.open("#aAlert");
                        $.get("/admin/goods/getformat/"+t.value, function(data, status, xhr){ 
                            $('#aformat').html('');
                            data = data.format
                            for(var i in data){
                                igft["igft"+data[i].id] = "gft"+data[i].id;
                                var stmp = '<div style="margin-bottom:5px;"><input id="igft{2}" style="width:70px;" class="gt" type="text" value="{0}" />：{1}</div>';
                                var sd = '';
                                data[i].KX = eval(data[i].KX);
                                for(var x in data[i].KX){
                                    sd += printf('<label><input onclick="selectft(this)" type="checkbox" name="gft{1}" /><input style="width:70px;" class="gt" type="text" value="{0}" name="xgft{1}" /></label>', data[i]['KX'][x], data[i].id);
                                }
                                $('#aformat').append(printf(stmp, data[i].name, sd, data[i].id));//label-primary
                            }
                            palert.close("#aAlert");
                            $("#aAlert").remove();
                        });
                    }
                    selectft = function(t){
                        $('#saddf').html('');
                        $('#saddb').html('');
                        var thr = [];
                        var bhr = [];
                        for (var i in igft){
                            var stmp = document.getElementsByName('x'+igft[i]);
                            var istmp = document.getElementsByName(igft[i]);
                            var isbhr = false;
                            var shbr = []
                            for(var j=0;j<stmp.length;j++){
                                if(istmp[j].checked){
                                    isbhr = true;
                                    if(thr.length <= 0){
                                        shbr.push(['<td>'+stmp[j].value+'</td>']);
                                    }else{
                                        var test = {}
                                        for(var x=0;x<bhr.length;x++){
                                            var ss = bhr[x].concat();
                                            if (ss.length>=2&&j>0){
                                                ss[ss.length-1] = '<td>'+stmp[j].value+'</td>';
                                            }else{
                                                ss.push('<td>'+stmp[j].value+'</td>');
                                            }
                                            if(test[ss.join()]==undefined){
                                                test[ss.join()] = 1;
                                                shbr.push(ss.concat());
                                            }
                                        }
                                    }
                                    bhr = shbr.concat();
                                }
                            }
                            if(isbhr){
                                thr.push('<th>'+document.getElementById(i).value+'</th>')
                            }
                        }
                        for(var i=0;i<thr.length;i++){
                            $('#saddf').append(thr[i]);
                        }
                        $('#saddf').append('<th>销售价</th><th>成本价</th><th>市场价</th><th>库存</th><th>是否启用</th>');
                        for(var i=0;i<bhr.length;i++){
                            var field = '<td><input class="gt" type="text" /></td><td><input class="gt" type="text" /></td><td><input class="gt" type="text" /></td><td><input class="gt" type="text" /></td><td><input type="checkbox" checked=true /></td>';
                            $('#saddb').append('<tr>'+bhr[i].join()+field+'</tr>');
                        }
                    }
                </script>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    addgoods = function(){
        attribute = [];
        attributename = document.getElementsByName("attributename");
        attributebody = document.getElementsByName("attribute");
        for(var i=0;i<attributename.length;i++){
            if(Trim(attributename[i].value)!=""){
                attribute.push({"name":attributename[i].value, "text":attributebody[i].value})
            }
        }
        if(Trim(document.getElementsByName('goodsname')[0].value)==""){
            return alert("请填写商品名称");
        }
        if(isNaN(parseFloat(document.getElementsByName('price')[0].value))||parseFloat(document.getElementsByName('price')[0].value)<=0){
            return alert("请填写正确的销售价");
        }
        if(isNaN(parseFloat(document.getElementsByName('vprice')[0].value))||parseFloat(document.getElementsByName('vprice')[0].value)<=0){
            return alert("请填写正确的市场价");
        }
        if(isNaN(parseFloat(document.getElementsByName('oprice')[0].value))||parseFloat(document.getElementsByName('oprice')[0].value)<=0){
            return alert("请填写正确的成本价");
        }
        if(isNaN(parseInt(document.getElementsByName('kc')[0].value))){
            return alert("请填写正确的库存");
        }
        $.post("/admin/goods/addgoods",{
            goodsname:document.getElementsByName('goodsname')[0].value, 
            upperid:document.getElementsByName('upperid')[0].value, 
            goodstitle:document.getElementsByName('goodstitle')[0].value, 
            sn:document.getElementsByName('sn')[0].value, 
            price:parseFloat(document.getElementsByName('price')[0].value), 
            vprice:parseFloat(document.getElementsByName('vprice')[0].value), 
            oprice:parseFloat(document.getElementsByName('oprice')[0].value), 
            kc:parseInt(document.getElementsByName('kc')[0].value), ///remark
            remark:document.getElementsByName('remark')[0].value, 
            isground:document.getElementsByName('isground')[0].checked?"1":"0", 
            isline:document.getElementsByName('islines')[0].checked?"1":"0", 
            text:ue.getContent(),
            image:UploadImg.getData('bpimg'),
            attribute:JSON.stringify(attribute),
        },function(data){

        });
    }
</script>